package edu.dlnu.newbootdemo.service;

import edu.dlnu.newbootdemo.dao.DluserMapper;
import edu.dlnu.newbootdemo.pojo.Dluser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :Lenovo
 * @description :
 * @create :2023/6/27
 */
@Service
public class UserServiceImpl implements IUserService {
    // 依赖注入DAO
    @Autowired
    private DluserMapper mapper;

    @Override
    public int login(Dluser user) {
        // 数据库查询的用户
        Dluser u = mapper.selectByPrimaryKey(user.getUserid());
        if(u == null) {
            return 2;
        } else {
            // 比较密码
            if(user.getUserpass().equals(u.getUserpass())) {
                if(user.getRole() == u.getRole()) {
                    return 1;
                } else return 4;
            } else {
                return 3;
            }
        }
    }
    // 注册逻辑
    @Override
    public boolean register(Dluser user) {
        if (user.getUserid().isEmpty() || user.getUserpass().isEmpty()) {
            System.out.println("账号或密码不能为空");
        }
        // 先查询
        Dluser u = mapper.selectByPrimaryKey(user.getUserid());
        // 后插入
        if (u == null) {
            return mapper.insert(user) > 0;
        } else return false;
    }

    @Override
    public List<Dluser> getUsers() {
        return mapper.selectAll();
    }
}
